<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/classes/BaseService.class.php, $Author:  $ */

/**
 * Base class for all modules services classes.
 * 
 * @package module
 *
 */
class BaseService extends Object
{
    /**
     * Owner Module
     *
     * @var BaseModule
     */
    var $_oModule;

    function BaseService($oModule)
    {
        $this->_oModule = $oModule;
        parent::Object();        
        $this->oDatabase = &Database::get();
	}

    /**  Performs callback actions
     *
     * @param object $oService      service name (short)
     * @param string $sCallMethod   callabck method
     * @param array  $aParams       parameters
     * @return boolean
     */
    function callback(&$oService, $sCallMethod, $aParams = array())
    {
        //$sSrvName = substr(get_class($oService), 23);
        if(preg_match("/.+_service(.+)/", strtolower(get_class($oService)), $aMatches))
        {
            $sSrvName = $aMatches[1];
        }
        else
        {
            return false;
        }
        $sCallbackFile = 'include/settings/callback/'.strtolower($this->_oModule->getName()).'.inc.php';
        $bReturns = true;
        if (file_exists($sCallbackFile))
        {
            require_once $sCallbackFile;
            if(isset($_CALLBACK[$sSrvName][$sCallMethod]))
            {
	            foreach ($_CALLBACK[$sSrvName][$sCallMethod] as $sCallback)
	            {
	                list($sModule, $sService, $sMethod) = explode('::', $sCallback);
	                $oSrvCallback = &App::getModuleService($sModule, $sService);
	                $bReturn = $oSrvCallback->$sMethod($aParams);
	                if (!$bReturn)
	                {
	                    $this->addErrors($oSrvCallback->getErrors());
	                }
	                if ($bReturns && !$bReturn)
	                    $bReturns = false;
	            }
            }
        }
        $sCacheCallBackFile = 'include/modules/'.$this->_oModule->getName().'/include/cache.inc.php';
        if (file_exists($sCacheCallBackFile))
        {
            require_once $sCacheCallBackFile;
            $this->_oModule->clearCache($_CACHEHTML[$sSrvName][$sCallMethod], $_CACHEDATA[$sSrvName][$sCallMethod]);
        }
        return $bReturns;
    }

    /**
     * Update data cache
     *
     * @param string $sServMethod
     * @param array $aArgs
     * @param mixed $mRes
     */
    function saveToDataCache($sServMethod, $aArgs, $mRes)
    {
        $sArgs = md5(serialize($aArgs));
        $mRes = serialize($mRes);
        $sFilePath = $this->_getDataCachePath($sServMethod, $sArgs);
        BaseComponent::_makeDir(dirname($sFilePath));
        $hCache = fopen($sFilePath, 'w');
        fwrite($hCache, $mRes);
        fclose($hCache);
    }

    /**
     * Read data cache
     *
     * @param string $sServMethod
     * @param array $aArgs
     * @return mixed
     */
    function getFromDataCache($sServMethod, $aArgs)
    {
        $mRes = null;
        $sArgs = md5(serialize($aArgs));
        $sFilePath = $this->_getDataCachePath($sServMethod, $sArgs);
        if (file_exists($sFilePath))
        {
            if (filectime($sFilePath) < (time() - $this->_oModule->_iTime))
                return null;
            $mRes = file_get_contents($sFilePath);
            $mRes = unserialize($mRes);
        }
        return $mRes;
    }

    /**
     * Returns path name with data for service method
     *
     * @param string $sServMethod
     * @param string $sArgs
     * @return string
     */
    function _getDataCachePath($sServMethod, $sArgs)
    {
        return $this->_oModule->getDataCacheDir(get_class($this), $sServMethod).$sArgs.'.dat';
    }
}
?>